
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <title>performUnitOfWork · React源码解析</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-api/theme-api.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

         
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="render-root.html" />
    
    
    <link rel="prev" href="perform-work.html" />
    

<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-49483742-5"
></script>
<script>
  window.dataLayer = window.dataLayer || []
  function gtag() {
    dataLayer.push(arguments)
  }
  gtag('js', new Date())

  gtag('config', 'UA-49483742-5')
</script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../api/react.html">
            
                <a href="../api/react.html">
            
                    
                    React API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../api/react-element.html">
            
                <a href="../api/react-element.html">
            
                    
                    ReactElement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../api/react-children.html">
            
                <a href="../api/react-children.html">
            
                    
                    React Children
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../api/react-structure.html">
            
                <a href="../api/react-structure.html">
            
                    
                    React 中的数据结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    创建更新
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../update/react-dom-render.html">
            
                <a href="../update/react-dom-render.html">
            
                    
                    ReactDOM.render
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../update/set-state.html">
            
                <a href="../update/set-state.html">
            
                    
                    setState & forceUpdate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../update/expiration-time.html">
            
                <a href="../update/expiration-time.html">
            
                    
                    expirationTime 计算
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    任务调度
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="scheduler-global.html">
            
                <a href="scheduler-global.html">
            
                    
                    全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="schedule-work.html">
            
                <a href="schedule-work.html">
            
                    
                    scheduleWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="scheduler-pkg.html">
            
                <a href="scheduler-pkg.html">
            
                    
                    react-scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="perform-work.html">
            
                <a href="perform-work.html">
            
                    
                    performWork
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.5" data-path="perform-unit-of-work.html">
            
                <a href="perform-unit-of-work.html">
            
                    
                    performUnitOfWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="render-root.html">
            
                <a href="render-root.html">
            
                    
                    renderRoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.6.1" data-path="render-root/throw-exception.html">
            
                <a href="render-root/throw-exception.html">
            
                    
                    throwException
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="begin-work.html">
            
                <a href="begin-work.html">
            
                    
                    beginWork
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.7.1" data-path="begin-work/mount-indeterminate-component.html">
            
                <a href="begin-work/mount-indeterminate-component.html">
            
                    
                    mountIndeterminateComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.2" data-path="begin-work/mount-lazy-component.html">
            
                <a href="begin-work/mount-lazy-component.html">
            
                    
                    mountLazyComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.3" data-path="begin-work/update-functional-component.html">
            
                <a href="begin-work/update-functional-component.html">
            
                    
                    updateFunctionalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.4" data-path="begin-work/update-class-component.html">
            
                <a href="begin-work/update-class-component.html">
            
                    
                    updateClassComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.5" data-path="begin-work/update-host-root.html">
            
                <a href="begin-work/update-host-root.html">
            
                    
                    updateHostRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.6" data-path="begin-work/update-host-component.html">
            
                <a href="begin-work/update-host-component.html">
            
                    
                    updateHostComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.7" data-path="begin-work/update-host-text.html">
            
                <a href="begin-work/update-host-text.html">
            
                    
                    updateHostText
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.8" data-path="../feature/Suspense-component.md">
            
                <span>
            
                    
                    updateSuspenseComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.9" data-path="begin-work/update-portal-component.html">
            
                <a href="begin-work/update-portal-component.html">
            
                    
                    updatePortalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.10" data-path="begin-work/update-forward-ref.html">
            
                <a href="begin-work/update-forward-ref.html">
            
                    
                    updateForwardRef
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.11" data-path="begin-work/update-fragment.html">
            
                <a href="begin-work/update-fragment.html">
            
                    
                    updateFragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.12" data-path="begin-work/update-mode.html">
            
                <a href="begin-work/update-mode.html">
            
                    
                    updateMode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.13" data-path="begin-work/update-profiler.html">
            
                <a href="begin-work/update-profiler.html">
            
                    
                    updateProfiler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.14" data-path="begin-work/update-context-provider.html">
            
                <a href="begin-work/update-context-provider.html">
            
                    
                    updateContextProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.15" data-path="begin-work/update-context-consumer.html">
            
                <a href="begin-work/update-context-consumer.html">
            
                    
                    updateContextConsumer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="reconcile-children.html">
            
                <a href="reconcile-children.html">
            
                    
                    reconcileChildren
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.8.1" data-path="reconcile-children/single.html">
            
                <a href="reconcile-children/single.html">
            
                    
                    调和单个节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8.2" data-path="reconcile-children/array.html">
            
                <a href="reconcile-children/array.html">
            
                    
                    调和列表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    commit 阶段
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../commit-phase/commit-root.html">
            
                <a href="../commit-phase/commit-root.html">
            
                    
                    commitRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../commit-phase/invokeGuardedCallback.html">
            
                <a href="../commit-phase/invokeGuardedCallback.html">
            
                    
                    invokeGuardedCallback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../commit-phase/commitBeforeMutationLifecycles.html">
            
                <a href="../commit-phase/commitBeforeMutationLifecycles.html">
            
                    
                    commitBeforeMutationLifecycles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../commit-phase/commit-all-host-effects.html">
            
                <a href="../commit-phase/commit-all-host-effects.html">
            
                    
                    commitAllHostEffects
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.4.1" data-path="../commit-phase/host-effects/CommitPlacement.html">
            
                <a href="../commit-phase/host-effects/CommitPlacement.html">
            
                    
                    commitPlacement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.2" data-path="../commit-phase/host-effects/CommitWork.html">
            
                <a href="../commit-phase/host-effects/CommitWork.html">
            
                    
                    commitWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.3" data-path="../commit-phase/host-effects/CommitDeletion.html">
            
                <a href="../commit-phase/host-effects/CommitDeletion.html">
            
                    
                    commitDeletion
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../commit-phase/commit-all-lifecycles.html">
            
                <a href="../commit-phase/commit-all-lifecycles.html">
            
                    
                    commitAllLifecycle
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    功能
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../features/context.html">
            
                <a href="../features/context.html">
            
                    
                    context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../features/hydrate.html">
            
                <a href="../features/hydrate.html">
            
                    
                    hydrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../features/ref.html">
            
                <a href="../features/ref.html">
            
                    
                    ref
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../features/event-injection.html">
            
                <a href="../features/event-injection.html">
            
                    
                    事件系统初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../features/event-bind.html">
            
                <a href="../features/event-bind.html">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../features/event-dispatch.html">
            
                <a href="../features/event-dispatch.html">
            
                    
                    事件触发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../features/event-create-event-object.html">
            
                <a href="../features/event-create-event-object.html">
            
                    
                    事件对象生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../features/Suspense-component.html">
            
                <a href="../features/Suspense-component.html">
            
                    
                    Suspense
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../features/suspense.html">
            
                <a href="../features/suspense.html">
            
                    
                    挂起任务的含义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../features/suspense-lazy.html">
            
                <a href="../features/suspense-lazy.html">
            
                    
                    lazy 组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Hooks
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../hooks/hooks-start.html">
            
                <a href="../hooks/hooks-start.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../hooks/hooks-use-state.html">
            
                <a href="../hooks/hooks-use-state.html">
            
                    
                    useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../hooks/hooks-use-effect.html">
            
                <a href="../hooks/hooks-use-effect.html">
            
                    
                    useEffect
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../hooks/hooks-others.html">
            
                <a href="../hooks/hooks-others.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../hooks/hooks-common.html">
            
                <a href="../hooks/hooks-common.html">
            
                    
                    通用API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <form
            action="https://buttondown.email/api/emails/embed-subscribe/Jokcy"
            method="post"
            target="popupwindow"
            onsubmit="window.open('https://buttondown.email/Jokcy', 'popupwindow')"
            class="buttondown-form"
            >
            <input type="email" name="email" id="bd-email" placeholder="(your@email.com)">
            <input type="hidden" value="1" name="embed"/>
            <button type="submit">订阅我</button>
        </form>
    </li>
</ul>

                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                 
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >performUnitOfWork</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="performunitofwork">performUnitOfWork</h1><div class="api-method"><div class="api-method-definition"><p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD8;&#x662F;&#x6BD4;&#x8F83;&#x597D;&#x7406;&#x89E3;&#x7684;</p><p>&#x9996;&#x5148;&#x6267;&#x884C;<code>beginWork</code>&#x8FDB;&#x884C;&#x8282;&#x70B9;&#x64CD;&#x4F5C;&#xFF0C;&#x4EE5;&#x53CA;&#x521B;&#x5EFA;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x5B50;&#x8282;&#x70B9;&#x4F1A;&#x8FD4;&#x56DE;&#x6210;&#x4E3A;<code>next</code>&#xFF0C;&#x5982;&#x679C;&#x6709;<code>next</code>&#x5C31;&#x8FD4;&#x56DE;&#x3002;&#x8FD4;&#x56DE;&#x5230;<code>workLoop</code>&#x4E4B;&#x540E;&#xFF0C;<code>workLoop</code>&#x4F1A;&#x5224;&#x65AD;&#x662F;&#x5426;&#x8FC7;&#x671F;&#x4E4B;&#x7C7B;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x90FD;OK&#x5C31;&#x4F1A;&#x518D;&#x6B21;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x3002;</p><p>&#x5982;&#x679C;<code>next</code>&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x8BF4;&#x660E;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x5411;&#x4E0B;&#x904D;&#x5386;&#x5B50;&#x8282;&#x70B9;&#x5DF2;&#x7ECF;&#x5230;&#x5E95;&#x4E86;&#xFF0C;&#x8BF4;&#x660E;&#x8FD9;&#x4E2A;&#x5B50;&#x6811;&#x4FA7;&#x679D;&#x5DF2;&#x7ECF;&#x904D;&#x5386;&#x5B8C;&#xFF0C;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x8FD9;&#x90E8;&#x5206;&#x5DE5;&#x4F5C;&#x4E86;&#x3002;&#x5C31;&#x6267;&#x884C;<code>completeUnitOfWork</code>&#xFF0C;<code>completeUnitOfWork</code>&#x5C31;&#x662F;&#x5904;&#x7406;&#x4E00;&#x4E9B;<code>effact tag</code>&#xFF0C;&#x4ED6;&#x4F1A;&#x4E00;&#x76F4;&#x5F80;&#x4E0A;&#x8FD4;&#x56DE;&#x76F4;&#x5230;<code>root</code>&#x8282;&#x70B9;&#x6216;&#x8005;&#x5728;&#x67D0;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x53D1;&#x73B0;&#x6709;<code>sibling</code>&#x5144;&#x5F1F;&#x8282;&#x70B9;&#x4E3A;&#x6B62;&#x3002;&#x5982;&#x679C;&#x5230;&#x4E86;<code>root</code>&#x90A3;&#x4E48;&#x4ED6;&#x7684;&#x8FD4;&#x56DE;&#x4E5F;&#x662F;<code>null</code>&#xFF0C;&#x4EE3;&#x8868;&#x6574;&#x68F5;&#x6811;&#x7684;&#x904D;&#x5386;&#x5DF2;&#x7ECF;&#x7ED3;&#x675F;&#x4E86;&#xFF0C;&#x53EF;&#x4EE5;<code>commit</code>&#x4E86;&#xFF0C;&#x5982;&#x679C;&#x9047;&#x5230;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#x5C31;&#x8FD4;&#x56DE;&#x8BE5;&#x8282;&#x70B9;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x8282;&#x70B9;&#x53EF;&#x80FD;&#x4E5F;&#x4F1A;&#x5B58;&#x5728;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;<code>beginWork</code>&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p><p><img alt="workLoop" src="https://i.postimg.cc/hPqjvNxr/work_Loop-v2.png"></p><ul><li><a href="perform-unit-of-work/begin-work.md">beginWork</a>
</li>
<li><a href="perform-unit-of-work/complete-unit-of-work.md">completeUnitOfWork</a>
</li></ul>
</div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">performUnitOfWork</span>(<span class="hljs-params">workInProgress: Fiber</span>): <span class="hljs-title">Fiber</span> | <span class="hljs-title">null</span> </span>{
  <span class="hljs-comment">// The current, flushed, state of this fiber is the alternate.</span>
  <span class="hljs-comment">// Ideally nothing should rely on this, but relying on it here</span>
  <span class="hljs-comment">// means that we don&apos;t need an additional field on the work in</span>
  <span class="hljs-comment">// progress.</span>
  <span class="hljs-keyword">const</span> current = workInProgress.alternate;

  <span class="hljs-comment">// See if beginning this work spawns more work.</span>
  startWorkTimer(workInProgress);
  <span class="hljs-keyword">if</span> (__DEV__) {
    ReactCurrentFiber.setCurrentFiber(workInProgress);
  }

  <span class="hljs-keyword">if</span> (__DEV__ &amp;&amp; replayFailedUnitOfWorkWithInvokeGuardedCallback) {
    stashedWorkInProgressProperties = assignFiberPropertiesInDEV(
      stashedWorkInProgressProperties,
      workInProgress,
    );
  }

  <span class="hljs-keyword">let</span> next;
  <span class="hljs-keyword">if</span> (enableProfilerTimer) {
    <span class="hljs-keyword">if</span> (workInProgress.mode &amp; ProfileMode) {
      startProfilerTimer(workInProgress);
    }

    next = beginWork(current, workInProgress, nextRenderExpirationTime);

    <span class="hljs-keyword">if</span> (workInProgress.mode &amp; ProfileMode) {
      <span class="hljs-comment">// Record the render duration assuming we didn&apos;t bailout (or error).</span>
      stopProfilerTimerIfRunningAndRecordDelta(workInProgress, <span class="hljs-literal">true</span>);
    }
  } <span class="hljs-keyword">else</span> {
    next = beginWork(current, workInProgress, nextRenderExpirationTime);
  }

  <span class="hljs-keyword">if</span> (__DEV__) {
    ReactCurrentFiber.resetCurrentFiber();
    <span class="hljs-keyword">if</span> (isReplayingFailedUnitOfWork) {
      <span class="hljs-comment">// Currently replaying a failed unit of work. This should be unreachable,</span>
      <span class="hljs-comment">// because the render phase is meant to be idempotent, and it should</span>
      <span class="hljs-comment">// have thrown again. Since it didn&apos;t, rethrow the original error, so</span>
      <span class="hljs-comment">// React&apos;s internal stack is not misaligned.</span>
      rethrowOriginalError();
    }
  }
  <span class="hljs-keyword">if</span> (__DEV__ &amp;&amp; ReactFiberInstrumentation.debugTool) {
    ReactFiberInstrumentation.debugTool.onBeginWork(workInProgress);
  }

  <span class="hljs-keyword">if</span> (next === <span class="hljs-literal">null</span>) {
    <span class="hljs-comment">// If this doesn&apos;t spawn new work, complete the current work.</span>
    next = completeUnitOfWork(workInProgress);
  }

  ReactCurrentOwner.current = <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">return</span> next;
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="beginwork">beginWork</h1><p>&#x9996;&#x5148;&#x6709;&#x4E00;&#x4E2A;&#x5224;&#x65AD;&#xFF0C;&#x8FD9;&#x4E2A;&#x5224;&#x65AD;&#x662F;&#x63D0;&#x5347;&#x6027;&#x80FD;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x70B9;&#xFF0C;&#x5982;&#x679C;&#x7B26;&#x5408;&#x8FD9;&#x4E2A;&#x6761;&#x4EF6;&#x90A3;&#x4E48;&#x8BF4;&#x660E;&#x8FD9;&#x4E2A;&#x8282;&#x70B9;&#x4EE5;&#x53CA;&#x4ED6;&#x7684;&#x5B50;&#x8282;&#x70B9;&#x5F88;&#x53EF;&#x80FD;&#x5728;&#x8FD9;&#x6B21;&#x66F4;&#x65B0;&#x4E2D;&#x90FD;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x88AB;&#x8BA1;&#x7B97;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x5185;&#x5BB9;</p><pre><code class="lang-js"><span class="hljs-keyword">const</span> updateExpirationTime = workInProgress.expirationTime
<span class="hljs-keyword">if</span> (
  !hasLegacyContextChanged() &amp;&amp;
  (updateExpirationTime === NoWork ||
    updateExpirationTime &gt; renderExpirationTime)
)</code></pre>
<p><code>hasLegacyContextChanged</code>&#x8FD9;&#x4E2A;&#x5224;&#x65AD;&#x662F;&#x7528;&#x6765;&#x5224;&#x65AD;<code>context</code>&#x7684;&#xFF0C;&#x8FD9;&#x4E2A;&#x5224;&#x65AD;&#x4EE3;&#x8868;&#x7740;&#x5982;&#x679C;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x548C;&#x4ED6;&#x7684;&#x7236;&#x8282;&#x70B9;&#x90FD;&#x6CA1;&#x6709;&#x8001;&#x7684;<code>context api</code>&#xFF0C;&#x4E5F;&#x5C31;&#x662F;<code>childContextType</code>&#x90A3;&#x5957;&#x7684;&#x8BDD;&#xFF0C;&#x4ED6;&#x5C31;&#x4E3A;<code>true</code>&#xFF0C;&#x4E0D;&#x7136;&#x4E0D;&#x7BA1;&#x4ED6;&#x7684;&#x7236;&#x7EA7;&#x7684;<code>context</code>&#x5185;&#x5BB9;&#x662F;&#x5426;&#x6709;&#x53D8;&#x5316;&#xFF0C;&#x4ED6;&#x90FD;&#x4E3A;<code>false</code>&#x3002;&#x6240;&#x4EE5;&#x8001;&#x7684;<code>context api</code>&#x5BF9;&#x6027;&#x80FD;&#x7684;&#x5F71;&#x54CD;&#x8FD8;&#x662F;&#x975E;&#x5E38;&#x5927;&#x7684;&#xFF0C;&#x8BE6;&#x7EC6;&#x53EF;&#x4EE5;&#x770B;<a href="../basic/context.md"><code>context</code></a></p><p>TODO: context api</p><p>&#x7B2C;&#x4E8C;&#x4E2A;&#x5224;&#x65AD;&#x662F;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x7684;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x7684;<code>expirationTime</code>&#x662F;<code>NoWork</code>&#x90A3;&#x4E48;&#x8BF4;&#x660E;&#x4ED6;&#x6CA1;&#x6709;&#x66F4;&#x65B0;&#xFF0C;&#x4E5F;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x6539;&#x52A8;&#x4E86;&#xFF0C;&#x5982;&#x679C;<code>expirationTime</code>&#x5927;&#x4E8E;&#x5F53;&#x524D;&#x6E32;&#x67D3;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x8BF4;&#x660E;&#x4ED6;&#x7684;&#x66F4;&#x65B0;&#x5185;&#x5BB9;&#x4E0D;&#x662F;&#x8FD9;&#x6B21;&#x66F4;&#x65B0;&#x9020;&#x6210;&#x7684;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x3002;</p><p>&#x5982;&#x679C;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#xFF0C;&#x6240;&#x6709;&#x7EC4;&#x4EF6;&#x90FD;&#x4F1A;&#x6267;&#x884C;<code>bailoutOnAlreadyFinishedWork</code>&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x4E8E;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;<code>context</code>&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x4ECD;&#x7136;&#x8981;&#x505A;&#x4E00;&#x4E9B;<code>context</code>&#x76F8;&#x5173;&#x7684;&#x64CD;&#x4F5C;</p><p><code>bailoutOnAlreadyFinishedWork</code>&#x5177;&#x4F53;&#x505A;&#x4E86;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;</p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">bailoutOnAlreadyFinishedWork</span>(<span class="hljs-params">
  current: Fiber | null,
  workInProgress: Fiber,
  renderExpirationTime: ExpirationTime,
</span>): <span class="hljs-title">Fiber</span> | <span class="hljs-title">null</span> </span>{
  cancelWorkTimer(workInProgress);

  <span class="hljs-keyword">if</span> (current !== <span class="hljs-literal">null</span>) {
    workInProgress.firstContextDependency = current.firstContextDependency;
  }

  <span class="hljs-keyword">if</span> (enableProfilerTimer) {
    stopProfilerTimerIfRunning(workInProgress);
  }

  <span class="hljs-keyword">const</span> childExpirationTime = workInProgress.childExpirationTime;
  <span class="hljs-keyword">if</span> (
    childExpirationTime === NoWork ||
    childExpirationTime &gt; renderExpirationTime
  ) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
  } <span class="hljs-keyword">else</span> {
    cloneChildFibers(current, workInProgress);
    <span class="hljs-keyword">return</span> workInProgress.child;
  }
}</code></pre>
<p>&#x6700;&#x4E3B;&#x8981;&#x7684;&#x662F;&#x770B;&#x6700;&#x540E;&#x9762;&#x7684;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;<code>childExpirationTime === NoWork || childExpirationTime &gt; renderExpirationTime</code>&#x6210;&#x7ACB;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4ED6;&#x7684;&#x5B50;&#x6811;&#x4E0A;&#x6CA1;&#x6709;&#x66F4;&#x65B0;&#xFF0C;&#x4ED6;&#x8FD4;&#x56DE;&#x7684;&#x662F;<code>null</code>&#xFF0C;&#x56DE;&#x60F3;&#x4E00;&#x4E0B;<code>performUnitOfWork</code>&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x8FD9;&#x91CC;<code>return null</code>&#x4EE3;&#x8868;&#x7740;<code>next</code>&#x662F;<code>null</code>&#xFF0C;&#x5C31;&#x76F4;&#x63A5;<code>completeUnitOfWork</code>&#x4E86;&#xFF0C;&#x5C31;&#x4E0D;&#x89E3;&#x6790;<code>child</code>&#x4E86;&#x3002;</p><p>&#x5982;&#x679C;&#x4E0D;&#x6EE1;&#x8DB3;&#x5C31;&#x662F;&#x590D;&#x7528;&#x5F53;&#x524D;<code>Fiber</code>&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x8FD4;&#x56DE;&#x4ED6;&#x7684;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x7684;&#x5B50;&#x8282;&#x70B9;&#x8FD8;&#x662F;&#x6709;&#x5DE5;&#x4F5C;&#x8981;&#x505A;&#x7684;&#x3002;</p><p>&#x7136;&#x540E;&#x5C31;&#x662F;&#x6839;&#x636E;<code>element</code>&#x7684;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x7684;<code>update</code>&#x3002;</p><p><strong>&#x8FD9;&#x91CC;&#x8BF4;&#x4E00;&#x4E0B;&#xFF0C;&#x5982;&#x679C;&#x51FA;&#x73B0;<code>switch(workInProgress.tag) case ClassComponentLazy</code>&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x8BF4;&#x660E;&#x8FD9;&#x4E2A;&#x5F02;&#x6B65;&#x7EC4;&#x4EF6;&#x5DF2;&#x7ECF;&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#x4E86;&#xFF0C;&#x5728;&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#x524D;&#x5E94;&#x8BE5;&#x90FD;&#x662F;<code>IndeterminateComponent</code>&#xFF0C;&#x4F1A;&#x76F4;&#x63A5;<code>throw</code>&#xFF0C;&#x9020;&#x6210;&#x8DDF;<code>Placeholder</code>&#x7EC4;&#x4EF6;&#x4F7F;&#x7528;&#x65F6;&#x4E00;&#x6837;&#x7684;&#x60C5;&#x51B5;</strong></p><ul><li><a href="begin-work/mount-indeterminate-component.html">mountIndeterminateComponent</a>
</li>
<li><a href="begin-work/update-functional-component.html">updateFunctionalComponent</a>
</li>
<li><a href="begin-work/update-class-component.html">updateClassComponent</a>
</li>
<li><a href="begin-work/update-host-root.html">updateHostRoot</a>
</li>
<li><a href="begin-work/update-host-component.html">updateHostComponent</a>
</li>
<li><a href="begin-work/update-host-text.html">updateHostText</a>
</li>
<li><a href="begin-work/update-timeout-component.html">updateTimeoutComponent</a>
</li>
<li><a href="begin-work/update-portal-component.html">updatePortalComponent</a>
</li>
<li><a href="begin-work/update-forward-ref.html">updateForwardRef</a>
</li>
<li><a href="begin-work/update-fragment.html">updateFragment</a>
</li>
<li><a href="begin-work/update-mode.html">updateMode</a>
</li>
<li><a href="begin-work/update-profiler.html">updateProfiler</a>
</li>
<li><a href="begin-work/update-context-provider.html">updateContextProvider</a>
</li>
<li><a href="begin-work/update-context-consumer.html">updateContextConsumer</a>
</li></ul>
</div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">beginWork</span>(<span class="hljs-params">
  current: Fiber | null,
  workInProgress: Fiber,
  renderExpirationTime: ExpirationTime,
</span>): <span class="hljs-title">Fiber</span> | <span class="hljs-title">null</span> </span>{
  <span class="hljs-keyword">const</span> updateExpirationTime = workInProgress.expirationTime;
  <span class="hljs-keyword">if</span> (
    !hasLegacyContextChanged() &amp;&amp;
    (updateExpirationTime === NoWork ||
      updateExpirationTime &gt; renderExpirationTime)
  ) {
    <span class="hljs-comment">// This fiber does not have any pending work. Bailout without entering</span>
    <span class="hljs-comment">// the begin phase. There&apos;s still some bookkeeping we that needs to be done</span>
    <span class="hljs-comment">// in this optimized path, mostly pushing stuff onto the stack.</span>
    <span class="hljs-keyword">switch</span> (workInProgress.tag) {
      <span class="hljs-keyword">case</span> HostRoot:
        pushHostRootContext(workInProgress);
        resetHydrationState();
        <span class="hljs-keyword">break</span>;
      <span class="hljs-keyword">case</span> HostComponent:
        pushHostContext(workInProgress);
        <span class="hljs-keyword">break</span>;
      <span class="hljs-keyword">case</span> ClassComponent: {
        <span class="hljs-keyword">const</span> Component = workInProgress.type;
        <span class="hljs-keyword">if</span> (isLegacyContextProvider(Component)) {
          pushLegacyContextProvider(workInProgress);
        }
        <span class="hljs-keyword">break</span>;
      }
      <span class="hljs-keyword">case</span> ClassComponentLazy: {
        <span class="hljs-keyword">const</span> thenable = workInProgress.type;
        <span class="hljs-keyword">const</span> Component = getResultFromResolvedThenable(thenable);
        <span class="hljs-keyword">if</span> (isLegacyContextProvider(Component)) {
          pushLegacyContextProvider(workInProgress);
        }
        <span class="hljs-keyword">break</span>;
      }
      <span class="hljs-keyword">case</span> HostPortal:
        pushHostContainer(
          workInProgress,
          workInProgress.stateNode.containerInfo,
        );
        <span class="hljs-keyword">break</span>;
      <span class="hljs-keyword">case</span> ContextProvider: {
        <span class="hljs-keyword">const</span> newValue = workInProgress.memoizedProps.value;
        pushProvider(workInProgress, newValue);
        <span class="hljs-keyword">break</span>;
      }
      <span class="hljs-keyword">case</span> Profiler:
        <span class="hljs-keyword">if</span> (enableProfilerTimer) {
          workInProgress.effectTag |= Update;
        }
        <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-keyword">return</span> bailoutOnAlreadyFinishedWork(
      current,
      workInProgress,
      renderExpirationTime,
    );
  }

  <span class="hljs-comment">// Before entering the begin phase, clear the expiration time.</span>
  workInProgress.expirationTime = NoWork;

  <span class="hljs-keyword">switch</span> (workInProgress.tag) {
    <span class="hljs-keyword">case</span> IndeterminateComponent: {
      <span class="hljs-keyword">const</span> Component = workInProgress.type;
      <span class="hljs-keyword">return</span> mountIndeterminateComponent(
        current,
        workInProgress,
        Component,
        renderExpirationTime,
      );
    }
    <span class="hljs-keyword">case</span> FunctionalComponent: {
      <span class="hljs-keyword">const</span> Component = workInProgress.type;
      <span class="hljs-keyword">const</span> unresolvedProps = workInProgress.pendingProps;
      <span class="hljs-keyword">return</span> updateFunctionalComponent(
        current,
        workInProgress,
        Component,
        unresolvedProps,
        renderExpirationTime,
      );
    }
    <span class="hljs-keyword">case</span> FunctionalComponentLazy: {
      <span class="hljs-keyword">const</span> thenable = workInProgress.type;
      <span class="hljs-keyword">const</span> Component = getResultFromResolvedThenable(thenable);
      <span class="hljs-keyword">const</span> unresolvedProps = workInProgress.pendingProps;
      <span class="hljs-keyword">const</span> child = updateFunctionalComponent(
        current,
        workInProgress,
        Component,
        resolveDefaultProps(Component, unresolvedProps),
        renderExpirationTime,
      );
      workInProgress.memoizedProps = unresolvedProps;
      <span class="hljs-keyword">return</span> child;
    }
    <span class="hljs-keyword">case</span> ClassComponent: {
      <span class="hljs-keyword">const</span> Component = workInProgress.type;
      <span class="hljs-keyword">const</span> unresolvedProps = workInProgress.pendingProps;
      <span class="hljs-keyword">return</span> updateClassComponent(
        current,
        workInProgress,
        Component,
        unresolvedProps,
        renderExpirationTime,
      );
    }
    <span class="hljs-keyword">case</span> ClassComponentLazy: {
      <span class="hljs-keyword">const</span> thenable = workInProgress.type;
      <span class="hljs-keyword">const</span> Component = getResultFromResolvedThenable(thenable);
      <span class="hljs-keyword">const</span> unresolvedProps = workInProgress.pendingProps;
      <span class="hljs-keyword">const</span> child = updateClassComponent(
        current,
        workInProgress,
        Component,
        resolveDefaultProps(Component, unresolvedProps),
        renderExpirationTime,
      );
      workInProgress.memoizedProps = unresolvedProps;
      <span class="hljs-keyword">return</span> child;
    }
    <span class="hljs-keyword">case</span> HostRoot:
      <span class="hljs-keyword">return</span> updateHostRoot(current, workInProgress, renderExpirationTime);
    <span class="hljs-keyword">case</span> HostComponent:
      <span class="hljs-keyword">return</span> updateHostComponent(current, workInProgress, renderExpirationTime);
    <span class="hljs-keyword">case</span> HostText:
      <span class="hljs-keyword">return</span> updateHostText(current, workInProgress);
    <span class="hljs-keyword">case</span> PlaceholderComponent:
      <span class="hljs-keyword">return</span> updatePlaceholderComponent(
        current,
        workInProgress,
        renderExpirationTime,
      );
    <span class="hljs-keyword">case</span> HostPortal:
      <span class="hljs-keyword">return</span> updatePortalComponent(
        current,
        workInProgress,
        renderExpirationTime,
      );
    <span class="hljs-keyword">case</span> ForwardRef: {
      <span class="hljs-keyword">const</span> type = workInProgress.type;
      <span class="hljs-keyword">return</span> updateForwardRef(
        current,
        workInProgress,
        type,
        workInProgress.pendingProps,
        renderExpirationTime,
      );
    }
    <span class="hljs-keyword">case</span> ForwardRefLazy:
      <span class="hljs-keyword">const</span> thenable = workInProgress.type;
      <span class="hljs-keyword">const</span> Component = getResultFromResolvedThenable(thenable);
      <span class="hljs-keyword">const</span> unresolvedProps = workInProgress.pendingProps;
      <span class="hljs-keyword">const</span> child = updateForwardRef(
        current,
        workInProgress,
        Component,
        resolveDefaultProps(Component, unresolvedProps),
        renderExpirationTime,
      );
      workInProgress.memoizedProps = unresolvedProps;
      <span class="hljs-keyword">return</span> child;
    <span class="hljs-keyword">case</span> Fragment:
      <span class="hljs-keyword">return</span> updateFragment(current, workInProgress, renderExpirationTime);
    <span class="hljs-keyword">case</span> Mode:
      <span class="hljs-keyword">return</span> updateMode(current, workInProgress, renderExpirationTime);
    <span class="hljs-keyword">case</span> Profiler:
      <span class="hljs-keyword">return</span> updateProfiler(current, workInProgress, renderExpirationTime);
    <span class="hljs-keyword">case</span> ContextProvider:
      <span class="hljs-keyword">return</span> updateContextProvider(
        current,
        workInProgress,
        renderExpirationTime,
      );
    <span class="hljs-keyword">case</span> ContextConsumer:
      <span class="hljs-keyword">return</span> updateContextConsumer(
        current,
        workInProgress,
        renderExpirationTime,
      );
    <span class="hljs-keyword">default</span>:
      invariant(
        <span class="hljs-literal">false</span>,
        <span class="hljs-string">&apos;Unknown unit of work tag. This error is likely caused by a bug in &apos;</span> +
          <span class="hljs-string">&apos;React. Please file an issue.&apos;</span>,
      );
  }
}
</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="completeunitofwork">completeUnitOfWork</h1><p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x8FD9;&#x91CC;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x5927;&#x7684;&#x5FAA;&#x73AF;&#xFF0C;&#x58F0;&#x660E;&#x4E86;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;<code>returnFiber</code>&#x662F;&#x7236;&#x4EB2;&#x8282;&#x70B9;&#xFF0C;<code>siblingFiber</code>&#x662F;&#x5144;&#x5F1F;&#x8282;&#x70B9;</p><p>&#x5FAA;&#x73AF;&#x5185;&#x90E8;&#x9996;&#x5148;&#x662F;&#x4E00;&#x4E2A;<code>if</code>&#x5224;&#x65AD;&#xFF0C;&#x8FD9;&#x4E2A;&#x5224;&#x65AD;&#x662F;&#x770B;&#x8FD9;&#x4E2A;&#x4EFB;&#x52A1;&#x662F;&#x5426;&#x662F;<code>Incomplete</code>&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x6536;&#x5426;&#x6709;&#x9519;&#x8BEF;&#x53D1;&#x751F;&#x5E76;&#x88AB;&#x6355;&#x83B7;&#x3002;</p><h3 id="&#x5982;&#x679C;&#x6CA1;&#x6709;&#x9519;&#x8BEF;">&#x5982;&#x679C;&#x6CA1;&#x6709;&#x9519;&#x8BEF;</h3><p>&#x5C31;&#x6267;&#x884C;<code>nextUnitOfWork = completeWork</code></p><p><a href="complete-unit-of-work/complete-work.md"><code>completeWork</code></a></p><p>&#x7136;&#x540E;&#x6267;&#x884C;<code>resetChildExpirationTime</code></p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">resetChildExpirationTime</span>(<span class="hljs-params">
  workInProgress: Fiber,
  renderTime: ExpirationTime,
</span>) </span>{
  <span class="hljs-keyword">if</span> (renderTime !== Never &amp;&amp; workInProgress.childExpirationTime === Never) {
    <span class="hljs-keyword">return</span>;
  }

  <span class="hljs-keyword">let</span> newChildExpirationTime = NoWork;

  <span class="hljs-comment">// if ProfileMode</span>

  {
    <span class="hljs-keyword">let</span> child = workInProgress.child;
    <span class="hljs-keyword">while</span> (child !== <span class="hljs-literal">null</span>) {
      <span class="hljs-keyword">const</span> childUpdateExpirationTime = child.expirationTime;
      <span class="hljs-keyword">const</span> childChildExpirationTime = child.childExpirationTime;
      <span class="hljs-keyword">if</span> (
        newChildExpirationTime === NoWork ||
        (childUpdateExpirationTime !== NoWork &amp;&amp;
          childUpdateExpirationTime &lt; newChildExpirationTime)
      ) {
        newChildExpirationTime = childUpdateExpirationTime;
      }
      <span class="hljs-keyword">if</span> (
        newChildExpirationTime === NoWork ||
        (childChildExpirationTime !== NoWork &amp;&amp;
          childChildExpirationTime &lt; newChildExpirationTime)
      ) {
        newChildExpirationTime = childChildExpirationTime;
      }
      child = child.sibling;
    }
  }

  workInProgress.childExpirationTime = newChildExpirationTime;
}</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x627E;&#x5230;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x7684;&#x6240;&#x6709;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x5E76;&#x4E14;&#x8BFB;&#x53D6;&#x4ED6;&#x7684;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#x548C;&#x4ED6;&#x7684;&#x5B50;&#x8282;&#x70B9;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF0C;&#x627E;&#x5230;&#x5176;&#x4E2D;&#x975E;<code>NoWork</code>&#x7684;&#x6700;&#x65E9;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#xFF0C;&#x7136;&#x540E;&#x8D4B;&#x503C;&#x7ED9;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x7684;<code>childExpirationTime</code></p><p>&#x7136;&#x540E;&#x5982;&#x679C;&#x6CA1;&#x6709;<code>next</code>&#x5C31;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#xFF0C;&#x5982;&#x679C;&#x6709;&#xFF0C;&#x5C31;&#x8981;&#x5411;&#x4E0A;&#x521B;&#x5EFA;<code>effect</code>&#x94FE;</p><p>&#x6700;&#x540E;&#x5982;&#x679C;&#x6709;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#xFF0C;&#x5BF9;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#x8FDB;&#x884C;<code>beginWork</code>&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5219;&#x7EE7;&#x7EED;&#x64CD;&#x4F5C;&#x4E0A;&#x7EA7;<code>fiber</code></p><h3 id="&#x5982;&#x679C;&#x6709;&#x9519;&#x8BEF;">&#x5982;&#x679C;&#x6709;&#x9519;&#x8BEF;</h3><p>&#x5219;&#x6267;&#x884C;<code>unwindWork</code>&#xFF0C;&#x540E;&#x7EED;&#x7684;&#x64CD;&#x4F5C;&#x8DDF;&#x6CA1;&#x6709;&#x9519;&#x8BEF;&#x7684;&#x60C5;&#x51B5;&#x5DEE;&#x4E0D;&#x591A;</p><p>&#x4F46;&#x662F;&#x591A;&#x4E86;&#x4E00;&#x6B65;&#xFF0C;&#x5C31;&#x662F;&#x4F1A;&#x5411;&#x4ED6;&#x7684;&#x7236;&#x8282;&#x70B9;&#x589E;&#x52A0;<code>Incomplete</code>&#x7684;<code>side effect tag</code></p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">completeUnitOfWork</span>(<span class="hljs-params">workInProgress: Fiber</span>): <span class="hljs-title">Fiber</span> | <span class="hljs-title">null</span> </span>{
  <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
    <span class="hljs-keyword">const</span> current = workInProgress.alternate;
    <span class="hljs-keyword">if</span> (__DEV__) {
      ReactCurrentFiber.setCurrentFiber(workInProgress);
    }

    <span class="hljs-keyword">const</span> returnFiber = workInProgress.return;
    <span class="hljs-keyword">const</span> siblingFiber = workInProgress.sibling;

    <span class="hljs-keyword">if</span> ((workInProgress.effectTag &amp; Incomplete) === NoEffect) {
      <span class="hljs-comment">// This fiber completed.</span>
      <span class="hljs-keyword">if</span> (enableProfilerTimer) {
        <span class="hljs-keyword">if</span> (workInProgress.mode &amp; ProfileMode) {
          startProfilerTimer(workInProgress);
        }

        nextUnitOfWork = completeWork(
          current,
          workInProgress,
          nextRenderExpirationTime,
        );

        <span class="hljs-keyword">if</span> (workInProgress.mode &amp; ProfileMode) {
          <span class="hljs-comment">// Update render duration assuming we didn&apos;t error.</span>
          stopProfilerTimerIfRunningAndRecordDelta(workInProgress, <span class="hljs-literal">false</span>);
        }
      } <span class="hljs-keyword">else</span> {
        nextUnitOfWork = completeWork(
          current,
          workInProgress,
          nextRenderExpirationTime,
        );
      }
      <span class="hljs-keyword">let</span> next = nextUnitOfWork;
      stopWorkTimer(workInProgress);
      resetChildExpirationTime(workInProgress, nextRenderExpirationTime);
      <span class="hljs-keyword">if</span> (__DEV__) {
        ReactCurrentFiber.resetCurrentFiber();
      }

      <span class="hljs-keyword">if</span> (next !== <span class="hljs-literal">null</span>) {
        stopWorkTimer(workInProgress);
        <span class="hljs-keyword">if</span> (__DEV__ &amp;&amp; ReactFiberInstrumentation.debugTool) {
          ReactFiberInstrumentation.debugTool.onCompleteWork(workInProgress);
        }
        <span class="hljs-comment">// If completing this work spawned new work, do that next. We&apos;ll come</span>
        <span class="hljs-comment">// back here again.</span>
        <span class="hljs-keyword">return</span> next;
      }

      <span class="hljs-keyword">if</span> (
        returnFiber !== <span class="hljs-literal">null</span> &amp;&amp;
        <span class="hljs-comment">// Do not append effects to parents if a sibling failed to complete</span>
        (returnFiber.effectTag &amp; Incomplete) === NoEffect
      ) {
        <span class="hljs-keyword">if</span> (returnFiber.firstEffect === <span class="hljs-literal">null</span>) {
          returnFiber.firstEffect = workInProgress.firstEffect;
        }
        <span class="hljs-keyword">if</span> (workInProgress.lastEffect !== <span class="hljs-literal">null</span>) {
          <span class="hljs-keyword">if</span> (returnFiber.lastEffect !== <span class="hljs-literal">null</span>) {
            returnFiber.lastEffect.nextEffect = workInProgress.firstEffect;
          }
          returnFiber.lastEffect = workInProgress.lastEffect;
        }

        <span class="hljs-keyword">const</span> effectTag = workInProgress.effectTag;
        <span class="hljs-keyword">if</span> (effectTag &gt; PerformedWork) {
          <span class="hljs-keyword">if</span> (returnFiber.lastEffect !== <span class="hljs-literal">null</span>) {
            returnFiber.lastEffect.nextEffect = workInProgress;
          } <span class="hljs-keyword">else</span> {
            returnFiber.firstEffect = workInProgress;
          }
          returnFiber.lastEffect = workInProgress;
        }
      }

      <span class="hljs-keyword">if</span> (__DEV__ &amp;&amp; ReactFiberInstrumentation.debugTool) {
        ReactFiberInstrumentation.debugTool.onCompleteWork(workInProgress);
      }

      <span class="hljs-keyword">if</span> (siblingFiber !== <span class="hljs-literal">null</span>) {
        <span class="hljs-comment">// If there is more work to do in this returnFiber, do that next.</span>
        <span class="hljs-keyword">return</span> siblingFiber;
      } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (returnFiber !== <span class="hljs-literal">null</span>) {
        <span class="hljs-comment">// If there&apos;s no more work in this returnFiber. Complete the returnFiber.</span>
        workInProgress = returnFiber;
        <span class="hljs-keyword">continue</span>;
      } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// We&apos;ve reached the root.</span>
        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
      }
    } <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">if</span> (workInProgress.mode &amp; ProfileMode) {
        <span class="hljs-comment">// Record the render duration for the fiber that errored.</span>
        stopProfilerTimerIfRunningAndRecordDelta(workInProgress, <span class="hljs-literal">false</span>);
      }

      <span class="hljs-keyword">const</span> next = unwindWork(workInProgress, nextRenderExpirationTime);
      <span class="hljs-comment">// Because this fiber did not complete, don&apos;t reset its expiration time.</span>
      <span class="hljs-keyword">if</span> (workInProgress.effectTag &amp; DidCapture) {
        <span class="hljs-comment">// Restarting an error boundary</span>
        stopFailedWorkTimer(workInProgress);
      } <span class="hljs-keyword">else</span> {
        stopWorkTimer(workInProgress);
      }

      <span class="hljs-keyword">if</span> (__DEV__) {
        ReactCurrentFiber.resetCurrentFiber();
      }

      <span class="hljs-keyword">if</span> (next !== <span class="hljs-literal">null</span>) {
        stopWorkTimer(workInProgress);
        <span class="hljs-keyword">if</span> (__DEV__ &amp;&amp; ReactFiberInstrumentation.debugTool) {
          ReactFiberInstrumentation.debugTool.onCompleteWork(workInProgress);
        }

        <span class="hljs-keyword">if</span> (enableProfilerTimer) {
          <span class="hljs-comment">// Include the time spent working on failed children before continuing.</span>
          <span class="hljs-keyword">if</span> (next.mode &amp; ProfileMode) {
            <span class="hljs-keyword">let</span> actualDuration = next.actualDuration;
            <span class="hljs-keyword">let</span> child = next.child;
            <span class="hljs-keyword">while</span> (child !== <span class="hljs-literal">null</span>) {
              actualDuration += child.actualDuration;
              child = child.sibling;
            }
            next.actualDuration = actualDuration;
          }
        }

        next.effectTag &amp;= HostEffectMask;
        <span class="hljs-keyword">return</span> next;
      }

      <span class="hljs-keyword">if</span> (returnFiber !== <span class="hljs-literal">null</span>) {
        <span class="hljs-comment">// Mark the parent fiber as incomplete and clear its effect list.</span>
        returnFiber.firstEffect = returnFiber.lastEffect = <span class="hljs-literal">null</span>;
        returnFiber.effectTag |= Incomplete;
      }

      <span class="hljs-keyword">if</span> (__DEV__ &amp;&amp; ReactFiberInstrumentation.debugTool) {
        ReactFiberInstrumentation.debugTool.onCompleteWork(workInProgress);
      }

      <span class="hljs-keyword">if</span> (siblingFiber !== <span class="hljs-literal">null</span>) {
        <span class="hljs-comment">// If there is more work to do in this returnFiber, do that next.</span>
        <span class="hljs-keyword">return</span> siblingFiber;
      } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (returnFiber !== <span class="hljs-literal">null</span>) {
        <span class="hljs-comment">// If there&apos;s no more work in this returnFiber. Complete the returnFiber.</span>
        workInProgress = returnFiber;
        <span class="hljs-keyword">continue</span>;
      } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
      }
    }
  }

  <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
}</code></pre>
</div></div></div>




                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
<div id="disqus_thread"></div>
<script>
  /**
   *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
   *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
  /*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
  ;(function() {
    // DON'T EDIT BELOW THIS LINE
    var d = document,
      s = d.createElement('script')
    s.src = 'https://react-source-code.disqus.com/embed.js'
    s.setAttribute('data-timestamp', +new Date())
    ;(d.head || d.body).appendChild(s)
  })()
</script>
<noscript
  >Please enable JavaScript to view the
  <a href="https://disqus.com/?ref_noscript"
    >comments powered by Disqus.</a
  ></noscript
>

            </div>

            

        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"performUnitOfWork","level":"1.4.5","depth":2,"next":{"title":"renderRoot","level":"1.4.6","depth":2,"path":"book/flow/render-root.md","ref":"book/flow/render-root.md","articles":[{"title":"throwException","level":"1.4.6.1","depth":3,"path":"book/flow/render-root/throw-exception.md","ref":"book/flow/render-root/throw-exception.md","articles":[]}]},"previous":{"title":"performWork","level":"1.4.4","depth":2,"path":"book/flow/perform-work.md","ref":"book/flow/perform-work.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-api"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-api":{"languages":[],"split":true,"theme":"light"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React源码解析","language":"zh-hans","gitbook":"*"},"file":{"path":"book/flow/perform-unit-of-work.md","mtime":"2018-10-09T12:01:27.038Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2019-07-08T12:28:32.264Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-api/theme-api.js"></script>
        
    

    </body>
</html>

